######################################
# Target
######################################
TARGET = h743_flight_control

######################################
# Building variables
######################################
# Debug build?
DEBUG = 1
# Optimization
OPT = -Og

#######################################
# Paths
#######################################
# Build path
BUILD_DIR = build

######################################
# Source
######################################
# C sources - With USB CDC and FreeRTOS
C_SOURCES =  \
Core/Src/main.c \
Core/Src/syscalls.c \
Core/Src/system_stm32h7xx.c \
Core/Src/stm32h7xx_it.c \
Drivers/HAL/stm32h7xx_hal.c \
Drivers/HAL/stm32h7xx_hal_cortex.c \
Drivers/HAL/stm32h7xx_hal_gpio.c \
Drivers/HAL/stm32h7xx_hal_rcc.c \
Drivers/HAL/stm32h7xx_hal_rcc_ex.c \
Drivers/HAL/stm32h7xx_hal_pwr.c \
Drivers/HAL/stm32h7xx_hal_pwr_ex.c \
Drivers/HAL/stm32h7xx_hal_flash.c \
Drivers/HAL/stm32h7xx_hal_flash_ex.c \
Drivers/HAL/stm32h7xx_hal_dma.c \
Drivers/HAL/stm32h7xx_hal_dma_ex.c \
Drivers/HAL/stm32h7xx_hal_tim.c \
Drivers/HAL/stm32h7xx_hal_tim_ex.c \
Drivers/HAL/stm32h7xx_hal_pcd.c \
Drivers/HAL/stm32h7xx_hal_pcd_ex.c \
Drivers/HAL/stm32h7xx_hal_sd.c \
Drivers/HAL/stm32h7xx_hal_sd_ex.c \
Drivers/HAL/stm32h7xx_hal_spi.c \
Drivers/HAL/stm32h7xx_hal_spi_ex.c \
Drivers/HAL/stm32h7xx_ll_usb.c \
Drivers/HAL/stm32h7xx_ll_sdmmc.c \
Middlewares/ST/STM32_USB_Device_Library/Core/Src/usbd_core.c \
Middlewares/ST/STM32_USB_Device_Library/Core/Src/usbd_ctlreq.c \
Middlewares/ST/STM32_USB_Device_Library/Core/Src/usbd_ioreq.c \
Middlewares/ST/STM32_USB_Device_Library/Core/Src/usbd_conf.c \
Middlewares/ST/STM32_USB_Device_Library/Core/Src/usbd_desc.c \
Middlewares/ST/STM32_USB_Device_Library/Class/CDC/Src/usbd_cdc.c \
Middlewares/ST/STM32_USB_Device_Library/Class/CDC/Src/usbd_cdc_if.c \
Middlewares/FreeRTOS/Source/event_groups.c \
Middlewares/FreeRTOS/Source/list.c \
Middlewares/FreeRTOS/Source/queue.c \
Middlewares/FreeRTOS/Source/stream_buffer.c \
Middlewares/FreeRTOS/Source/tasks.c \
Middlewares/FreeRTOS/Source/timers.c \
Middlewares/FreeRTOS/Source/portable/GCC/ARM_CM7/r0p1/port.c \
Middlewares/FreeRTOS/Source/portable/MemMang/heap_4.c \
Middlewares/Third_Party/FatFs/src/ff.c \
Middlewares/Third_Party/FatFs/src/ff_gen_drv.c \
Middlewares/Third_Party/FatFs/src/diskio.c \
Middlewares/Third_Party/FatFs/src/option/syscall.c \
Middlewares/Third_Party/FatFs/src/option/unicode.c \
Drivers/BSP/bsp_sd_diskio.c \
Drivers/BSP/bsp_spi.c \
Drivers/Sensors/bmi088.c \
Drivers/Sensors/bmi270.c \
Middlewares/PX4_Platform/src/px4_sem.c \
Middlewares/PX4_Platform/src/hrt_call.c \
Middlewares/PX4_Platform/src/intrusive_queue.c \
Middlewares/PX4_Platform/src/intrusive_sorted_list.c \
Middlewares/PX4_Platform/test/test_px4_sem.c \
Middlewares/PX4_Platform/test/test_hrt_call.c \
Middlewares/PX4_Platform/test/test_intrusive_queue.c \
Middlewares/PX4_Platform/test/test_intrusive_sorted_list.c \
Middlewares/PX4_Platform/test/test_freertos_api.c

# C++ sources
CXX_SOURCES = \
Middlewares/PX4_Platform/test/test_lock_guard_cpp.cpp \
Middlewares/PX4_Platform/test/test_blocking_list.cpp \
Middlewares/PX4_Platform/test/test_blocking_queue.cpp \
Middlewares/PX4_Platform/src/work_queue_manager.cpp \
Middlewares/PX4_Platform/src/work_queue.cpp \
Middlewares/PX4_Platform/test/test_work_queue_manager.cpp \
Middlewares/PX4_Platform/src/work_item.cpp \
Middlewares/PX4_Platform/test/test_work_item.cpp \
Middlewares/PX4_Platform/src/scheduled_work_item.cpp \
Middlewares/PX4_Platform/test/test_scheduled_work_item.cpp \
Middlewares/PX4_Platform/src/work_item_single_shot.cpp \
Middlewares/PX4_Platform/test/test_work_item_single_shot.cpp \
Middlewares/PX4_Platform/test/test_integration.cpp

# ASM sources
ASM_SOURCES =  \
Core/Src/startup_stm32h743xx.s

#######################################
# Binaries
#######################################
PREFIX = arm-none-eabi-
CC = $(PREFIX)gcc
CXX = $(PREFIX)g++
AS = $(PREFIX)gcc -x assembler-with-cpp
CP = $(PREFIX)objcopy
SZ = $(PREFIX)size
HEX = $(CP) -O ihex
BIN = $(CP) -O binary -S
 
#######################################
# CFLAGS
#######################################
# CPU
CPU = -mcpu=cortex-m7

# FPU
FPU = -mfpu=fpv5-d16

# Float-abi
FLOAT-ABI = -mfloat-abi=hard

# MCU
MCU = $(CPU) -mthumb $(FPU) $(FLOAT-ABI)

# AS defines
AS_DEFS = 

# C defines
C_DEFS =  \
-DUSE_HAL_DRIVER \
-DSTM32H743xx \
-DARM_MATH_CM7 \
-D__FPU_PRESENT=1

# AS includes
AS_INCLUDES = 

# C includes - With USB CDC, FreeRTOS, FatFs and PX4 Platform
C_INCLUDES =  \
-ICore/Inc \
-IDrivers/HAL \
-IDrivers/BSP \
-IDrivers/Sensors \
-IMiddlewares/ST/STM32_USB_Device_Library/Core/Inc \
-IMiddlewares/ST/STM32_USB_Device_Library/Class/CDC/Inc \
-IMiddlewares/FreeRTOS/Source/include \
-IMiddlewares/FreeRTOS/Source/portable/GCC/ARM_CM7/r0p1 \
-IMiddlewares/Third_Party/FatFs/src \
-IMiddlewares/PX4_Platform/include \
-IConfig

# Compile gcc flags
ASFLAGS = $(MCU) $(AS_DEFS) $(AS_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections

CFLAGS = $(MCU) $(C_DEFS) $(C_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections

# C++ flags (based on PX4 settings)
CXXFLAGS = $(MCU) $(C_DEFS) $(C_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections
CXXFLAGS += -std=c++14 -fno-exceptions -fno-rtti -fno-threadsafe-statics

ifeq ($(DEBUG), 1)
CFLAGS += -g -gdwarf-2
CXXFLAGS += -g -gdwarf-2
endif

# Generate dependency information
CFLAGS += -MMD -MP -MF"$(@:%.o=%.d)"
CXXFLAGS += -MMD -MP -MF"$(@:%.o=%.d)"

#######################################
# LDFLAGS
#######################################
# Link script
LDSCRIPT = STM32H743VIHx_FLASH.ld

# Libraries
LIBS = -lc -lm -lnosys 
LIBDIR = 
LDFLAGS = $(MCU) -specs=nano.specs -T$(LDSCRIPT) $(LIBDIR) $(LIBS) -Wl,-Map=$(BUILD_DIR)/$(TARGET).map,--cref -Wl,--gc-sections

# Default action: build all
all: $(BUILD_DIR)/$(TARGET).elf $(BUILD_DIR)/$(TARGET).hex $(BUILD_DIR)/$(TARGET).bin

#######################################
# Build the application
#######################################
# List of objects
OBJECTS = $(addprefix $(BUILD_DIR)/,$(notdir $(C_SOURCES:.c=.o)))
vpath %.c $(sort $(dir $(C_SOURCES)))
# List of C++ objects
OBJECTS += $(addprefix $(BUILD_DIR)/,$(notdir $(CXX_SOURCES:.cpp=.o)))
vpath %.cpp $(sort $(dir $(CXX_SOURCES)))
# List of ASM program objects
OBJECTS += $(addprefix $(BUILD_DIR)/,$(notdir $(ASM_SOURCES:.s=.o)))
vpath %.s $(sort $(dir $(ASM_SOURCES)))

$(BUILD_DIR)/%.o: %.c Makefile | $(BUILD_DIR)
	$(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@

$(BUILD_DIR)/%.o: %.cpp Makefile | $(BUILD_DIR)
	$(CXX) -c $(CXXFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(notdir $(<:.cpp=.lst)) $< -o $@

$(BUILD_DIR)/%.o: %.s Makefile | $(BUILD_DIR)
	$(AS) -c $(CFLAGS) $< -o $@

$(BUILD_DIR)/$(TARGET).elf: $(OBJECTS) Makefile
	$(CXX) $(OBJECTS) $(LDFLAGS) -o $@
	$(SZ) $@

$(BUILD_DIR)/%.hex: $(BUILD_DIR)/%.elf | $(BUILD_DIR)
	$(HEX) $< $@
	
$(BUILD_DIR)/%.bin: $(BUILD_DIR)/%.elf | $(BUILD_DIR)
	$(BIN) $< $@	
	
$(BUILD_DIR):
	mkdir $@		

#######################################
# Clean
#######################################
clean:
	-rm -fR $(BUILD_DIR)

#######################################
# Flash
#######################################
flash: all
	openocd -f interface/stlink.cfg -f target/stm32h7x.cfg -c "program $(BUILD_DIR)/$(TARGET).elf verify reset exit"

#######################################
# Debug
#######################################
debug:
	openocd -f interface/stlink.cfg -f target/stm32h7x.cfg

#######################################
# Dependencies
#######################################
-include $(wildcard $(BUILD_DIR)/*.d)

# *** EOF ***

